package study.datastructure.stack.leetcode;

import java.util.Stack;

/**
 * 232
 * @author 86188
 */
public class QueueUsingTwoStacks {

    private Stack<Integer> stack1;
    private Stack<Integer> stack2;

    public QueueUsingTwoStacks() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    // 入队操作
    public void enqueue(int item) {
        stack1.push(item);
    }

    // 出队操作
    public int dequeue() {
        if (isEmpty()) {
            throw new RuntimeException("Queue is empty");
        }

        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }

        return stack2.pop();
    }

    // 判断队列是否为空
    public boolean isEmpty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }

    public static void main(String[] args) {
        QueueUsingTwoStacks queue = new QueueUsingTwoStacks();

        queue.enqueue(1);
        queue.enqueue(2);
        queue.enqueue(3);

        System.out.println("出队元素: " + queue.dequeue());
        System.out.println("出队元素: " + queue.dequeue());

        queue.enqueue(4);

        System.out.println("出队元素: " + queue.dequeue());
        System.out.println("出队元素: " + queue.dequeue());
    }
}